package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.PartyOrganization;
import com.smart.village.big.screen.domain.PartyOrganizationTenureAttachment;
import com.smart.village.big.screen.domain.PartyOrganizationTenureStaticVo;
import com.smart.village.big.screen.mapper.PartyOrganizationMapper;
import com.smart.village.big.screen.mapper.PartyOrganizationTenureMapper;
import com.smart.village.big.screen.service.PartyOrganizationTenureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PartyOrganizationTenureServiceImpl implements PartyOrganizationTenureService {
    @Autowired
    private PartyOrganizationTenureMapper partyOrganizationTenureMapper;
    @Autowired
    private PartyOrganizationMapper partyOrganizationMapper;

    @Override
    public JSONObject getPartyOrganizationTenure(String partyOrganizationSerialNumber) {
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(partyOrganizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(partyOrganizationSerialNumber,"");
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }

        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
        });

        List<PartyOrganizationTenureStaticVo> partyOrganizationTenureList = Lists.newArrayList();
        for (String poSerialNumber : treePartyOrganizationSerialNumberList) {
            PartyOrganizationTenureStaticVo partyOrganizationTenure = partyOrganizationTenureMapper.getPartyOrganizationTenure(poSerialNumber);
            if (!ObjectUtils.isEmpty(partyOrganizationTenure)) {
                partyOrganizationTenureList.add(partyOrganizationTenure);
            }
        }
        JSONObject resultJson = new JSONObject();
        if (CollectionUtils.isEmpty(partyOrganizationTenureList)) {
            resultJson.put("total", 0);
            resultJson.put("partyOrganizationTenureList", Lists.newArrayList());
            resultJson.put("finish", 0);
            resultJson.put("unfinished", 0);
            resultJson.put("preparing", 0);
            resultJson.put("will", 0);
        } else {
            resultJson.put("total", partyOrganizationTenureList.size());
            Map<String, Long> statusCounts = partyOrganizationTenureList.parallelStream().collect(Collectors.groupingBy(PartyOrganizationTenureStaticVo::getTenureStatus, Collectors.counting()));
            resultJson.put("partyOrganizationTenureList", partyOrganizationTenureList);
            resultJson.put("finish", !statusCounts.containsKey("已完成") ? 0 : statusCounts.get("已完成"));
            resultJson.put("unfinished", !statusCounts.containsKey("未完成") ? 0 : statusCounts.get("未完成"));
            resultJson.put("preparing", !statusCounts.containsKey("筹备中") ? 0 : statusCounts.get("筹备中"));
            resultJson.put("will", !statusCounts.containsKey("未开始") ? 0 : statusCounts.get("未开始"));
        }

        return resultJson;
    }

    @Override
    public JSONObject getPartyOrganizationTenureDetail(String tenureSerialNumber) {
        JSONObject jsonObject = new JSONObject();
        List<PartyOrganizationTenureAttachment> meetList =
        partyOrganizationTenureMapper.getMeetFileListByTenureSerialNumber(tenureSerialNumber);
        List<PartyOrganizationTenureAttachment> approvalList =
        partyOrganizationTenureMapper.getApprovalFileListByTenureSerialNumber(tenureSerialNumber);
        jsonObject.put("meetList", meetList);
        jsonObject.put("approvalList", approvalList);
        return jsonObject;
    }

    @Override
    public List<JSONObject> getAllGovernmentStructure(String townNumber, String villageNumber) {
        return partyOrganizationMapper.getAllGovernmentStructure(townNumber, villageNumber);
    }
}
